package com.bocai.grainshop.seckill.interceptor;

import com.bocai.common.constant.AuthServerConstant;
import com.bocai.common.vo.MemberVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class SeckillInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberVo> loginUser = new ThreadLocal<>();


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //通过路径匹配放行 ware服务发来的远程调用
        boolean match = new AntPathMatcher().match("/kill", request.getRequestURI());
        if(match){
            Object attribute = request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
            if(attribute!=null){
                MemberVo memberVo = (MemberVo) attribute;
                loginUser.set(memberVo);
                return true;
            }else {
                request.getSession().setAttribute("msg","请先登录");
                response.sendRedirect("http://auth.grainshop.com/login");
                return false;
            }
        }
        return true;

    }
}
